<?php
namespace app\controller\admin\system\category;

use app\BaseController;
use app\common\model\user\User;
use think\App;
use app\common\repositories\system\CategoryRepository as repository;

class Category extends BaseController
{
    protected $repository;

    public function __construct(App $app, repository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }

    public function lst()
    {
        $type = input('type');
        return app('json')->success($this->repository->getFormatList($type));
    }


    public function createForm($type)
    {
        return app('json')->success(formToData($this->repository->form($type)));
    }


    public function updateForm($id)
    {
        if (!$data = $this->repository->get($id))
            return app('json')->fail('数据不存在');
        return app('json')->success(formToData($this->repository->updateForm($data->type,$id)));
    }

    public function create()
    {
        $data = $this->checkParams();
        $data['cate_name'] = trim($data['cate_name']);
        if($data['cate_name'] == '')  return app('json')->fail('分类名不可为空');

        if ($data['pid'] && !$this->repository->exists($data['type'],$data['pid']))
            return app('json')->fail('上级分类不存在');
        if ($data['pid'] && !$this->repository->checkLevel($data['pid'],$data['type']))
            return app('json')->fail('不可添加更低阶分类');
        $this->repository->create($data);
        $this->repository->delCache($data['type']);
        return app('json')->success('添加成功');
    }


    public function update($id)
    {
        $data = $this->checkParams();
        if(!$this->repository->checkUpdate($id,$data['pid'])){
            if (!$this->repository->exists($data['type'], $id))
                return app('json')->fail('数据不存在');
            if ($data['pid'] && !$this->repository->exists($data['type'], $data['pid']))
                return app('json')->fail('上级分类不存在');
            if ($data['pid'] && !$this->repository->checkLevel($data['pid'],$data['type']))
                return app('json')->fail('不可添加更低阶分类');
            if (!$this->repository->checkChangeToChild($id,$data['pid']))
                return app('json')->fail('无法修改到当前分类到子集，请先修改子类');
            if (!$this->repository->checkChildLevel($id,$data['pid']))
                return app('json')->fail('子类超过最低限制，请先修改子类');
        }

        $this->repository->delCache($data['type']);
        unset($data['type']);
        $this->repository->update($id,$data);

        return app('json')->success('编辑成功');
    }


    public function switchStatus($id)
    {
        $status = $this->request->param('status', 0) == 1 ? 1 : 0;
        if (!$data = $this->repository->get($id))
            return app('json')->fail('数据不存在');

        $this->repository->switchStatus($id, $status);
        $this->repository->delCache($data->type);
        return app('json')->success('修改成功');
    }


    public function delete($id)
    {
        if (!$data = $this->repository->get($id))
            return app('json')->fail('数据不存在');
        if ($this->repository->hasChild($id))
            return app('json')->fail('该分类存在子集，请先处理子集');

        $this->repository->delete($id);
        $this->repository->delCache($data->type);
        return app('json')->success('删除成功');
    }


    public function detail($id)
    {
        if (!$this->repository->merExists($this->request->merId(), $id))
            return app('json')->fail('数据不存在');
        return app('json')->success($this->repository->get($id));
    }


    public function checkParams()
    {
        $data = $this->request->params(['pid','cate_name','is_show','pic','sort','type']);
        validate(
            ['pid|父级分类' => 'require|integer',
            'cate_name|分类名称' => 'require|max:50',
            'is_show|状态' => 'require|in:0,1',
            'pic|分类图标' => 'max:128',
            'sort|排序' => 'require|integer']
           // 'type|类型' => 'require|in:'.join(',',array_keys($this->repository::TYPE_MAP))]
        )->check($data);
        return $data;
    }


    public function getTreeList($type)
    {
        return app('json')->success($this->repository->getTreeList($type));
    }


    public function getCateType()
    {
        return success($this->repository::TYPE_MAP);
    }

    public function getList($type)
    {
        return app('json')->success($this->repository->getList($type));
    }


}
